package com.wangtao.firstapp.adapters;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.wangtao.firstapp.R;
import com.wangtao.firstapp.activity.ShopDetailsActivity;
import com.wangtao.firstapp.modles.ShopDetailsData;

import org.xutils.common.util.DensityUtil;
import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.List;

/**
 * 商品详情页面-->相关好物 的适配器
 * Created by lanouhn on 16/9/10.
 */
public class ShopWellAdapter extends BaseAdapter {

    private Context context;
    private List<ShopDetailsData.DataBean.ProductListBean> productListBeen;

    public ShopWellAdapter(Context context, List<ShopDetailsData.DataBean.ProductListBean> productListBeen) {
        this.context = context;
        this.productListBeen = productListBeen;
    }

    @Override
    public int getCount() {
        return productListBeen.size();
    }

    @Override
    public Object getItem(int position) {
        return productListBeen.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        convertView = LayoutInflater.from(context).inflate(R.layout.item_mygridview4, null);
        ImageView imageView = (ImageView) convertView.findViewById(R.id.iv_item_mygridview4);

        ImageOptions imageOptions = new ImageOptions.Builder()
                .setSize(DensityUtil.dip2px(80), DensityUtil.dip2px(80))//图片大小
                //.setRadius(DensityUtil.dip2px(5))//ImageView圆角半径
                .setCrop(true)// 如果ImageView的大小不是定义为wrap_content, 不要crop.
                .setImageScaleType(ImageView.ScaleType.CENTER_CROP)
                .setLoadingDrawableId(R.mipmap.ic_launcher)//加载中默认显示图片
                .setFailureDrawableId(R.mipmap.ic_launcher)//加载失败后默认显示图片
                .build();
        //imageView.setPadding(3, 0, 1, 25);
        x.image().bind(imageView, productListBeen.get(position).getPic(), imageOptions);


        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String shop_ID = productListBeen.get(position).getId();
                Intent intent = new Intent(context, ShopDetailsActivity.class);
                intent.putExtra("shop_iD", shop_ID);
                context.startActivity(intent);

            }
        });

        return convertView;

    }
}
